//
//  TradeOrderUserCenterViewModel.swift
//  Exchange
//
//  Created by rui on 2018/6/14.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class TradeOrderUserCenterViewModel: BaseViewModel {
    
    var userAdPage: Int = 1
    
    
    var userRelatedPage: Int = 1
    
    let size: Int = 15
    
    var userAdIsNoMoreData = false
    
    var userRelatedIsNoMoreData = false
    
    var userAdList = [AdvertisingListModel]()
    
    var userInfoModel: AdvertisingListModel?
    
    ///用户id
    var userId: Int = 0
    
    var userRelatedList = [TradeOrderListModel]()
    
    private lazy var userAdApi: TradeUserAdRequest = {
        let api = TradeUserAdRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    private lazy var userRelated: TradeUserRelatedRequest = {
        let api = TradeUserRelatedRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    private lazy var defriendApi: TradeUserDefriendRequest = {
        let api = TradeUserDefriendRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    private lazy var cancelDefriendApi: TradeUserCancelDefriendRequest = {
        let api = TradeUserCancelDefriendRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    private lazy var trustApi: TradeUserTrustRequest = {
        let api = TradeUserTrustRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    private lazy var cancelTrustApi: TradeUserCancelTrustRequest = {
        let api = TradeUserCancelTrustRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    
    private lazy var userInfo: TradeUserInfoRequest = {
        let api = TradeUserInfoRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    
    
    
    ///信任某人
    func trust() {
        self.trustApi.loadData()
    }
    
    ///取消信任
    func cancelTrust() {
        self.cancelTrustApi.loadData()
    }
    ///取消拉黑
    func cancleDefriend() {
        self.cancelDefriendApi.loadData()
    }
    
    ///拉黑
    func defriend() {
        self.defriendApi.loadData()
    }
    
    func addUserId(id: Int) {
        self.userId = id
    }
    
    func loadUserInfo() {
        self.userInfo.loadData()
    }
    
    func loadNewUserAdData()  {
        self.userAdPage = 1
        self.userAdApi.loadData()
    }
    
    func loadMoreUserAdData() {
        self.userAdPage += 1
        self.userAdApi.loadData()
    }
    
    
    func loadNewUserRelatedData()  {
        self.userAdPage = 1
        self.userRelated.loadData()
    }
    
    func loadMoreUserRelatedData() {
        self.userAdPage += 1
        self.userRelated.loadData()
    }
    
}

extension TradeOrderUserCenterViewModel: RequestCallBack,RequestDataReformer,RequestParamSource {
    
    func reform(_ request: BaseRequest, _ data: [String : Any]?) {
        if let _ = request as? TradeUserRelatedRequest {
            var flag = false
            if let realyData = data?["data"] as? [String:Any] {
                if let list = realyData["list"] as? [[String:Any]] {
                    if let jsonData = list.jsonData {
                        do{
                     
                                if self.userRelatedPage == 1 {
                                    self.userRelatedList.removeAll()
                                }
                            let models = try JSONDecoder().decode(Array<TradeOrderListModel>.self, from: jsonData)
                            
                            if let hasNext =  realyData["hasNextPage"] as? Bool {
                                self.userRelatedIsNoMoreData = !hasNext
                            }
                            
                            self.userRelatedList.append(contentsOf: models)
                            flag = true
                        }catch {
                            flag = false
                            dPrint(error)
                        }
                    }
                }
            }
            if !flag {
                self.userRelatedPage -= 1
            }
            self.response?(ResponseState.trade_order_related,flag, nil)
        }else if let _ = request as? TradeUserAdRequest {
            var flag = false
            if let realyData = data?["data"] as? [String:Any] {
                if let list = realyData["list"] as? [[String:Any]] {
                    if let jsonData = list.jsonData {
                        do{
                            if self.userAdPage == 1 {
                                self.userAdList.removeAll()
                            }
                            let models = try JSONDecoder().decode(Array<AdvertisingListModel>.self, from: jsonData)
                            
                            if let hasNext =  realyData["hasNextPage"] as? Bool {
                                self.userAdIsNoMoreData = !hasNext
                            }
                            
                            self.userAdList.append(contentsOf: models)
                            flag = true
                        }catch {
                            flag = false
                            dPrint(error)
                        }
                    }
                }
            }
            if !flag {
                self.userAdPage -= 1
            }
            self.response?(ResponseState.trade_ad_user,flag, nil)
        }else if let _ = request as? TradeUserInfoRequest {
            dPrint(data)
            var flag = false
            if let realyData = data?["data"] as? [String:Any] {
                if let jsonData = realyData.jsonData {
                    do{
                        let model = try JSONDecoder().decode(AdvertisingListModel.self, from: jsonData)
                        self.userInfoModel = model
                        flag = true
                    }catch {
                        flag = false
                        dPrint(error)
                    }
                }
            }
            self.response?(ResponseState.trade_ad_userInfo,flag, nil)
        }else if let _ = request as? TradeUserDefriendRequest {
             self.response?(ResponseState.mine_defriend,true, nil)
        }else if let _ = request as? TradeUserTrustRequest {
              self.response?(ResponseState.mine_trust,true, nil)
        }else if let _ = request as? TradeUserCancelTrustRequest {
              self.response?(ResponseState.mine_cancel_trust,true, nil)
        }else if let _ = request as? TradeUserCancelDefriendRequest {
            self.response?(ResponseState.mine_cancel_defriend,true, nil)
        }
    }
    
    func requestDidSucess(_ request: BaseRequest) {
        if let _ = request as? TradeUserAdRequest {
            self.userAdIsNoMoreData = false

        }else if let _ = request as? TradeUserRelatedRequest {
            self.userRelatedIsNoMoreData = false
        }
    }
    
    func requestDidFail(_ request: BaseRequest) {
        
        if let _ = request as? TradeUserAdRequest {
            self.userAdIsNoMoreData = false
            self.userAdPage -= 1
            self.response?(ResponseState.trade_ad_user,false, request.errorMsg)
        }else if let _ = request as? TradeUserRelatedRequest {
            self.userRelatedIsNoMoreData = false
            self.userRelatedPage -= 1
            self.response?(ResponseState.trade_order_related,false, request.errorMsg)
        }else if let _ = request as? TradeUserInfoRequest {
            self.response?(ResponseState.trade_ad_userInfo,false, request.errorMsg)
        }else if let _ = request as? TradeUserDefriendRequest {
            self.response?(ResponseState.mine_defriend,false, nil)
        }else if let _ = request as? TradeUserTrustRequest {
            self.response?(ResponseState.mine_trust,false, nil)
        }else if let _ = request as? TradeUserCancelTrustRequest {
            self.response?(ResponseState.mine_cancel_trust,false, nil)
        }else if let _ = request as? TradeUserCancelDefriendRequest {
            self.response?(ResponseState.mine_cancel_defriend,false, nil)
        }
    }
    
    func requestParams(_ request: BaseRequest) -> [String : Any]? {
        var pramas: [String:Any] = [:]
        if let _ = request as? TradeUserAdRequest {
            pramas.updateValue(size, forKey: "size")
            pramas.updateValue(self.userAdPage, forKey: "page")
            pramas.updateValue(self.userId, forKey: "userId")
        }else if let _ = request as? TradeUserRelatedRequest {
            pramas.updateValue(size, forKey: "size")
            pramas.updateValue(self.userRelatedPage, forKey: "page")
            pramas.updateValue(self.userId, forKey: "userId")
        }else if let _ = request as? TradeUserInfoRequest {
            pramas.updateValue(self.userId, forKey: "userId")
        }else if let _ = request as? TradeUserCancelTrustRequest {
            pramas.updateValue(AppInfo.default.userId ?? 0, forKey: "fromUserId")
            pramas.updateValue(self.userId, forKey: "toUserId")
        }else if let _ = request as? TradeUserTrustRequest {
            pramas.updateValue(AppInfo.default.userId ?? 0, forKey: "fromUserId")
            pramas.updateValue(self.userId, forKey: "toUserId")
        }else if let _ = request as? TradeUserDefriendRequest {
            pramas.updateValue(AppInfo.default.userId ?? 0, forKey: "fromUserId")
            pramas.updateValue(self.userId, forKey: "toUserId")
        }else if let _ = request as? TradeUserCancelDefriendRequest {
            pramas.updateValue(AppInfo.default.userId ?? 0, forKey: "fromUserId")
            pramas.updateValue(self.userId, forKey: "toUserId")
        }
        return pramas
    }
    
}
